//
//  CompressedHelper.swift
//  KXUnzip
//
//  Created by Li Dong on 2022/11/11.
//

import UIKit



class CompressedHelper: NSObject {

    static let shared = CompressedHelper()
    
    let filePath = "/Downloads/mm_files"
    

    
    func mainPath() -> String {
//        let dicPath = NSHomeDirectory().appending(filePath)
        let dicPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).last ?? "" + filePath
        if !FileManager.default.fileExists(atPath: dicPath) {
            try? FileManager.default.createDirectory(atPath: dicPath, withIntermediateDirectories: true, attributes: nil)
        }
        return dicPath
    }
    
    func getFilePath(fileName: String) -> String {
        return mainPath() + "/\(fileName)"
    }
    
    func writeData(data: Data, fileName: String) {
        let path = getFilePath(fileName: fileName)
        if FileManager.default.fileExists(atPath: path) {
            do {
                try  FileManager.default.removeItem(at: URL(fileURLWithPath: path))
            } catch let e {
                print("删除失败 = ", e.localizedDescription)
            }
        }
        do {
            try data.write(to: URL(fileURLWithPath:path))
        }catch let e {
            print("保存失败 = ", e.localizedDescription)
        }
    }
    
    func deleteFile(fileName: String)  {
        deleteFile(path: getFilePath(fileName: fileName))
    }
    
    
    func deleteFile(path: String)  {
        if FileManager.default.fileExists(atPath: path) {
            do {
                try FileManager.default.removeItem(at: URL(fileURLWithPath: path))
            } catch let e {
                print("删除失败", e.localizedDescription)
            }
        }
    }
    
    func moveFile(sourcePath: String, toPath: String) {
        if FileManager.default.fileExists(atPath: sourcePath) {
            do {
                try FileManager.default.moveItem(atPath: sourcePath, toPath: toPath)
            } catch let e {
                print("move失败", e.localizedDescription)
            }
        }
    }
    
    func copyFile(atPath: String, fileName: String) {
        if FileManager.default.fileExists(atPath: atPath) {
            if FileManager.default.fileExists(atPath: atPath) {
                try? FileManager.default.createDirectory(atPath: mainPath() , withIntermediateDirectories: true, attributes: nil)
            }
            do {
                try FileManager.default.copyItem(atPath: atPath, toPath: mainPath() + "/\(fileName)")
            } catch let e {
                print("copy失败", e.localizedDescription)
            }
        }
    }

    func tempFoderPath() -> String {
        return mainPath()
    }
    
    func deleteTempFolder() {
        if FileManager.default.fileExists(atPath: mainPath() ) {
            do {
                try FileManager.default.removeItem(atPath: mainPath() )
            } catch let e {
                print("删除临时文件失败", e.localizedDescription)
            }
        }
    }

    
    
    func sizeFormat(size: Int) -> String {
        if size < 1024 {
            return "\(size)B"
        }else if size >= 1024 && size < 1024*1024 {
            return "\(size/1024)KB"
        }else if size >= 1024*1024 && size < 1024*1024*1024 {
            return String(format: "%.1fM", CGFloat(size)/(1024*1024))
        }else {
            return String(format: "%.1fG", CGFloat(size)/(1024*1024*1024))
        }
    }
    
    func getTimeStr(timestamp: Int) -> String {
        return df.string(from: Date(timeIntervalSince1970: TimeInterval(timestamp)))
    }
    
    lazy var df: DateFormatter = {
        let temp = DateFormatter()
        temp.dateFormat = "yyyy.MM.dd HH:mm:ss"
        return temp
    }()
}
